<?php
/*
fileHashDB
Copyright (c) 2010 The Hursk Group, LLC. All rights reserved.

www.hursk.com

hurskgroup@hursk.com

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

*/
require 'db_functions.php';
require 'common_functions.php';

$start_time_epoch = time();

// load config file	and check for local copy
if (file_exists("../local_config.php")) {
	require '../local_config.php';
} else if(file_exists("../config.php")) {
	require '../config.php';
} else {
	$xml = c_initiate_xml($config_data);
	$xml_error = $xml->addchild('error');
	$xml_error->addchild('code', '500');
	$xml_error->addchild('description', 'No config file found.');
	c_announce($xml);
}

// make db connection
$db_link = dbf_connectDB($config_data);


// mother large query.  We'll echo time before and after.
echo "Start distinct query ".time();
$query = "SELECT DISTINCT MD5 FROM NSRLFile"; // get a list of unique md5 values
$result = mysqli_query($db_link, $query);
echo "End distinct query ".time();

// for each unique entry we will push them into the common has table
// we do this becasue there are multiple entries in the NSRL has db for
// aparently multiple version of the same file (different product or 
// os codes)

while ($row = mysqli_fetch_assoc($result)) {
	//print_r($row);
	
	$query = "SELECT SHA1, CRC32 FROM NSRLFile WHERE MD5 = '$row[MD5]'";
	$result2 = mysqli_query($db_link, $query);
	$row2 = mysqli_fetch_assoc($result2);
	//print_r($row2);
	
	// put into the db
	$query = "INSERT INTO fileHash (sha1, md5, crc32, NSRL) VALUES ('$row2[SHA1]', '$row[MD5]', '$row2[CRC32]', '1')";
	$result3 = mysqli_query($db_link, $query);
	
}

?>